package com.youlu.campus.admin.jinshuju;


import com.youlu.campus.admin.annotation.RequestActivityAuth;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.service.jinshuju.JinShuJuActivityService;
import com.youlu.campus.service.jinshuju.JinShuJuUserRankService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Map;

@Slf4j
@RestController
public class JinShuJuActivityController {

    @Autowired
    private JinShuJuActivityService jinShuJuActivityService;
    @Autowired
    private JinShuJuUserRankService jinShuJuUserRankService;

    @PostMapping("/activity/jinshuju/create")
    public QResult createActivity(@RequestBody @NotNull ActivityInfo request) {
        request.setCreatorId(AuthUtils.getUserId());
        jinShuJuActivityService.save(request);
        return QResult.success(null);
    }

    @PostMapping("/activity/jinshuju/save")
    @RequestActivityAuth(activityId = "#request.id")
    public QResult updateActivity(@RequestBody @NotNull ActivityInfo request) {
        jinShuJuActivityService.save(request);
        return QResult.success(null);
    }

    /**
     * 导入贡献值
     *
     * @param
     * @return
     */
    @PostMapping("/activity/jinshuju/q/import")
//    @RequestActivityAuth(activityId = "#activityId")
    public RestResult shipImport(@RequestParam("file") MultipartFile file,
                                 @RequestParam("activityId") String activityId,
                                 @RequestParam("comments") String comments) {
        try {
            String fileName = file.getOriginalFilename();
            log.info(":>>> 批量导入贡献值文件:{}", fileName);
            Map<String, Object> res = jinShuJuUserRankService.qImport(file.getBytes(), fileName, activityId, comments, AuthUtils.getLoginName());
            return RestResultBuilder.builder().success(res).build();
        } catch (Exception e) {
            log.info(":>>> 批量导入贡献值文件错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }
}
